#include<iostream>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<string>
#include<cstdlib>
#include<cerrno>
#include<cstring>

#define MODE 0666

const std::string path = "./sharedM.cpp";
const int proj_id = 0Xffff4332;

int main()
{
  //获取唯一key
  key_t key = ftok(path.c_str(), proj_id);
  if (key == -1)
  {
    exit(-1);
  }


  //获取共享内存
  int shmid = shmget(key, 4096, IPC_CREAT);
  if (shmid == -1)
  {
    exit(-1);
  }

  //挂接
  char* shmadder = (char*)shmat(shmid, nullptr, 0);

  if (shmadder == nullptr)
  {
    std::cout << "nullptr" << std::endl;
    exit(-1);
  }
  //开始通讯
  
  while (true)
  {
    std::string t;
    std::cout << "server:  ";
    std::getline(std::cin, t);
    strncpy(shmadder, t.c_str(), t.size() + 1);
    if (t == "nullptr")
    {
      break;
    }
  }


  
  //去挂接
  int c = shmdt(shmadder);
  if (c == -1)
  {
    exit(-1);
  }

  return 0;
}
